<template>
  <div>
    <Row :gutter="10">
      <Col>
      <Card>
        <div class="live_wrapper clearfix"
             style="margin-top: 10px">
          <Input v-model="filter.content"
                 @on-search="search"
                 search
                 enter-button="搜索"
                 placeholder="请输入评价内容"
                 style="display:inline-table; width: 210px;" />
          <DatePicker type="daterange"
                      v-model="created_at"
                      @on-change="search"
                      placement="bottom-end"
                      placeholder="选择评价时间"
                      style="width: 200px; margin-left: 10px;"></DatePicker>
          <Select v-model="filter.type"
                  style="width: 200px;margin-left: 10px;"
                  @on-change="search"
                  placeholder="请选择评价类型">
            <Option value=0>全部</Option>
            <Option value=1>好评</Option>
            <Option value=2>中评</Option>
            <Option value=3>差评</Option>
            <Option value=4>晒图</Option>
            <Option value=5>视频</Option>
          </Select>
          <Button type="primary"
                  @click.stop="exportData()"
                  style="float:right; width: 80px;height: 34px;font-size: 14px; margin-left: 10px">导出</Button>
        </div>
        <p class="admin_title">
          全部评价
        </p>
        <Row class="margin-top-10 table_pattern table_pattern2">
          <Table style="width: calc(100vw - 256px - 36px - 32px)"
                 :loading="loading"
                 :columns="columns"
                 :data="list"></Table>
        </Row>
        <Row class="margin-top-10">
          <Page :total="pagenation.totalPage"
                :page-size="pagenation.perPage"
                :current="pagenation.currentPage"
                show-elevator
                show-total
                @on-change="change"></Page>
        </Row>
      </Card>
      </Col>
    </Row>
    <Modal v-model="show_order_reply"
           title="评价回复"
           width="500px"
           :mask-closable="false"
           class="goods_modal cyj_modal">
      <Form :label-width="90">
        <FormItem label="回复内容"
                  prop="content_reply"
                  class="label_required">
          <Input type="textarea"
                 :rows="4"
                 v-model="content_reply"
                 placeholder="请输入回复内容" />
        </FormItem>
        <FormItem>
          <Button type="primary"
                  @click="orderReplySubmit"
                  style="width: 90px;height: 35px;border-radius: 0;font-size: 14px; margin-left:80px">提交</Button>
        </FormItem>
      </Form>
    </Modal>
    <Modal title="查看图片"
           v-model="visible"
           class="visible_modal">
      <swiper :options="swiperOption"
              ref="swiperOption"
              style="height: 600px;">
        <swiper-slide v-for="(it2,idx) in imgList"
                      :key="idx">
          <img :src="it2"
               alt=""
               width="100%"
               style="height: 600px;-o-object-fit: contain;object-fit: contain;" />
        </swiper-slide>
        <div class="swiper-button-prev"
             slot="button-prev"
             v-show="imgList.length > 1"></div>
        <div class="swiper-button-next"
             slot="button-next"
             v-show="imgList.length > 1"></div>
      </swiper>
    </Modal>
  </div>
</template>
<script>
import config from '@/config'
import Time from '@/tool/Time'
import { orderEvaluateList, orderReply, changeOrderEvaluateStatus } from '@/api/main'
export default {
  data () {
    return {
      pagenation: {},
      columns: [
        {
          key: 'user_phone',
          title: '用户',
          render: (h, params) => {
            return h('strong', params.row.user_phone)
          },
        },
        {
          key: 'good_name',
          title: '商品',
          render: (h, params) => {
            return h('strong', params.row.good_name)
          },
        },
        {
          key: 'content',
          title: '内容',
          render: (h, params) => {
            return h('strong', params.row.content)
          },
        },
        {
          key: 'imgs',
          title: '图片',
          align: 'center',
          render: (h, params) => {
            let arr = []
            if (params.row.imgs.length) {
              arr.push(h(
                'img',
                {
                  style: {
                    width: '100px',
                    height: '100px',
                    margin: '10px 0',
                    position: 'relative',
                    cursor: 'pointer',
                    objectFit: 'cover',
                  },
                  attrs: {
                    src: params.row.imgs[0]
                  },
                  on: {
                    click: () => {
                      this.imgList = []
                      this.imgList = params.row.imgs
                      this.visible = true
                      this.$refs.swiperOption.$swiper.slideTo(0, 1000, false)
                    }
                  }
                }
              ))
              return h(
                'div',
                {
                  style: {
                    overflow: 'hidden'
                  },
                },
                arr
              )
            }
          },
        },
        {
          key: 'video',
          title: '视频',
          render: (h, params) => {
            return h('strong', params.row.video)
          },
        },
        {
          key: 'created_time',
          title: '评价时间',
          render: (h, params) => {
            return h('strong', params.row.created_time)
          },
        },
        {
          title: '操作',
          align: 'center',
          width: 120,
          render: (h, params) => {
            return h('div', [
              /*h(
                'Button',
                {
                  props: {
                    size: 'small'
                  },
                  style: {
                    marginBottom: '5px',
                    marginRight: '5px',
                    border: 'none',
                    background: 'transparent',
                    fontSize: '14px',
                    color: '#377EEF'
                  },
                  on: {
                    click: () => {
                      this.orderReply(params.row)
                    },
                  },
                },
                '回复',
              ),*/
              h(
                'Poptip',
                {
                  props: {
                    confirm: true,
                    title: '您确定要删除该条评价吗?',
                    transfer: true,
                  },
                  on: {
                    'on-ok': () => {
                      let obj = {}
                      obj.id = params.row.id
                      obj.status = -1
                      changeOrderEvaluateStatus(obj).then(res => {
                        if (res.code === 0) {
                          let _this = this
                          this.$Message.success('删除成功')
                          let item = _this.list.find(item => {
                            return item.id === params.row.id
                          })
                          let index = _this.list.indexOf(item)
                          _this.list.splice(index, 1)
                        } else {
                          this.$Message.success(res.text)
                        }
                      })
                    },
                  },
                },
                [
                  h(
                    'Button',
                    {
                      style: {
                        margin: '0 5px 5px 0',
                        border: 'none',
                        background: 'transparent',
                        fontSize: '14px',
                        color: '#377EEF'
                      },
                      props: {
                        placement: 'top',
                        size: 'small',
                      },
                    },
                    '删除',
                  ),
                ],
              ),
            ])
          },
        },
      ],
      list: [],
      loading: true,
      filter: {
        page: 0,
        page_size: 20,
        content: '',
        created_range: '',
        order_id: 0,
        type: -1
      },
      show_order_reply: false,
      content_reply: '',
      created_at: ['', ''],
      cur_obj: {},
      imgList: [],
      visible: false,
      swiperOption: {
        // 设置点击箭头
        navigation: {
          nextEl: ".swiper-button-next",
          prevEl: ".swiper-button-prev"
        }
      },
    }
  },
  watch: {
    'created_at': function () {
      let created_at_start = Time.dateToTimestamp(this.created_at[0])
      let created_at_end = Time.dateToTimestamp(this.created_at[1])
      if (created_at_start !== 0 && created_at_end !== 0) {
        this.filter.created_range = created_at_start + '-' + created_at_end
      } else {
        this.filter.created_range = ''
      }
    }
  },
  methods: {
    change: function (page) {
      this.loading = true
      this.filter.page = page - 1
      this.init()
    },
    search: function () {
      this.filter.page = 0
      this.init()
    },
    orderReply (item) {
      let _this = this
      _this.cur_obj = item
      _this.show_order_reply = true
    },
    orderReplySubmit () {
      let _this = this
      if (this.content_reply == '') {
        this.$Message.error("请输入回复内容"); return;
      }
      let obj = {}
      obj.id = _this.cur_obj.id
      obj.content_reply = _this.content_reply
      orderReply(obj).then(res => {
        if (res.code == 0) {
          _this.$Message.success('操作成功')
          _this.show_order_reply = false
          _this.init()
        } else {
          _this.$Message.error(res.text)
        }
      })
    },
    init () {
      orderEvaluateList(this.filter).then(res => {
        if (res.code == 0) {
          this.list = res.data
          this.loading = false
          this.pagenation.currentPage = parseInt(res.currentPage) + 1
          this.pagenation.totalPage = parseInt(res.totalPage)
          this.pagenation.perPage = parseInt(res.perPage)
        }
      })
    },
    formatJson (filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    exportData () {
      let _this = this
      _this.filter.page = 0
      _this.filter.page_size = 500
      _this.export_list = []
      _this.export()
    },
    export () {
      let _this = this
      orderEvaluateList(this.filter).then(res => {
        if (res.code == 0) {
          if (res.data.length) {
            _this.export_list.push.apply(_this.export_list, res.data)
            _this.filter.page++
            _this.export()
          } else {
            _this.filter.page_size = 20
            const { export_json_to_excel } = require('../../excel/Export2Excel');
            const tHeader = ['用户', '商品', '评价内容', '评价时间'];
            const filterVal = ['user_name', 'good_name', 'content', 'created_time'];
            const data = _this.formatJson(filterVal, _this.export_list)
            export_json_to_excel(tHeader, data, '订单评价信息')
          }
        }
      })
    },
  },
  mounted () {
    this.filter.order_id = this.$route.params.order_id
    this.init()
  }
}
</script>
<style lang="less">
@page {
	margin-top: 1mm; //去掉页眉
	margin-bottom: 1mm; //去掉页脚
}
.edui-notadd .edui-dialog {
	z-index: 2005 !important;
}
.edui-notadd {
	z-index: 2002 !important;
}
</style>
<style scoped lang="less">
/deep/ .edui-notadd .edui-dialog {
	z-index: 5000 !important;
}
/deep/ input::-webkit-input-placeholder {
	color: rgba(153, 153, 153, 0.47) !important;
	font-size: 14px !important;
}
/deep/ input::-moz-placeholder {
	color: rgba(153, 153, 153, 0.47) !important;
	font-size: 14px !important;
}
/deep/ input:-moz-placeholder {
	color: rgba(153, 153, 153, 0.47) !important;
	font-size: 14px !important;
}
/deep/ input:-ms-input-placeholder {
	color: rgba(153, 153, 153, 0.47) !important;
	font-size: 14px !important;
}
</style>
